<?php
class Poll_Widget_ProfileQuestionsController extends Engine_Content_Widget_Abstract
{
    protected $_childCount;
    public function indexAction() {
        $viewer = Engine_Api::_()->user()->getViewer();
        if( !Engine_Api::_()->core()->hasSubject() ) {
            return $this->setNoRender();
        }
        
        $subject = Engine_Api::_()->core()->getSubject();
        if( !$subject->authorization()->isAllowed($viewer, 'view') ) {
            return $this->setNoRender();
        }
        
        $request = Zend_Controller_Front::getInstance()->getRequest();
        
        //$this->getElement()->removeDecorator('Title');
        $isSelf = ($subject->getIdentity() == $viewer->getIdentity()) ? true : false;
        if (!$isSelf) $isFriend = Engine_Api::_()->poll()->isFriend($subject, $viewer);        
        $tb = Engine_Api::_()->getDbTable('Polls', 'Poll');    
        $sl = $tb->select()->where('user_id = ?', $subject->getIdentity());

        
        if (!$isSelf && $isFriend != 1) $sl->where('privacy = "public"');
        $this->view->paginator = $paginator = Zend_Paginator::factory($sl);
        $paginator->setItemCountPerPage(10);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        if( !$paginator->getTotalItemCount() ) {
            return $this->setNoRender();
        }
        $this->_childCount = $paginator->getTotalItemCount();
    }
    
    public function getChildCount() {
        return $this->_childCount;
    }
}
